<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="pub/top_list2.jsp"%>

<%
String userid = (String) request.getSession().getAttribute("name");
Object[] params = new Object[2];
params[0] =userid;
params[1] =userid; 
String sql = "select a.sximg,b.oprice,a.sxname,c.classify,d.* from goodsx a, goodjiage b,shuxing c,shopping d where a.goodsxid=b.goodsxid and a.goodsxid=d.goodsxid and a.sxid=c.sxid and d.userid=?  and a.goodsxid in (select goodsxid from shopping where userid=?)";
List<Map<String, Object>> list= db.queryList(sql, params);
String li=JSON.toJSONString(list);
//System.out.println(list);

%>

<script>
</script>
<body>
	<header class="page-header">
		<h3>购物车</h3>
	</header>

	<div class="contaniner fixed-contb">
	       <%
	       String shopmxid="";
	       String oprice="";
	       int a=0;
	       int b=0;
			    for (int i=0;i<list.size();i++) {
				Map m = (Map)list.get(i);
				String sximg = m.get("sximg").toString();
				String classify=m.get("classify").toString();
				String sxname=m.get("sxname").toString();
			    oprice=m.get("oprice").toString();
				shopmxid=m.get("shopmxid").toString();
				String count=m.get("count").toString();
				a++;
				b++;
		    %>
		<section class="shopcar" >
			<div class="shopcar-checkbox">
				<!-- <label  for="shopcar" onselectstart="return false" ></label> <input type="checkbox" > -->
			</div>
			<figure> 
				<img src="<%=sximg %>" />
			</figure>
			<dl>
				<dd><%=classify%>：<%=sxname %></dd>
				<div class="add">
					<span class="<%=shopmxid %>" onclick="funr(this)">-</span> <input type="text" readonly value="<%=count %>" id="<%=shopmxid %>"/> <span class="<%=shopmxid %>" onclick="funa(this)">+</span>
				</div>
				<h3 class="<%=shopmxid %>"><%=oprice %></h3>
				<small><img src="images/shopcar-icon01.png" class="<%=shopmxid %>" onclick="shanchu(this)"/></small>
				<span style="display:none"></span>
			</dl>
		</section>
		<%} %>
		<div style="margin-bottom: 16%;"></div>
	</div>
	<script type="text/javascript">
	$(document).ready(function() {
		fun();
	});
  
    function fun(){
	   var totalprice= 0.00;
	   var list=<%=li%>;
	   var allp;
	   for(var i in list){
		   var num = document.getElementById(list[i].shopmxid);
		   var sss=Number(num.value)*Number(list[i].oprice);
		   totalprice=Number(totalprice)+sss;
	   } 
	   document.getElementById("totalprice").innerHTML=totalprice
   }
   
   function funr(a){
	   var state=3;
	   var cn=a.className;
	   var input_num = document.getElementById(cn);
	   if(input_num.value <= 0) {
           input_num.value = 0;
       } else {
           input_num.value = parseInt(input_num.value) - 1;
       } 
	   fun();
	   $.ajax({
			type : 'post',
			url : 'test.jsp',
			data : {"shopmxid":cn,"state":state,"count":input_num.value},
			success : function(data) {	
			}
		});
   }
   function funa(a){
	   var state=4;
	   var cn=a.className;
	   var totalprice= document.getElementById("totalprice").innerHTML;
	   var input_num = document.getElementById(cn);
	   //console.log(input_num)
	   input_num.value = parseInt(input_num.value) + 1;
	   fun();
	   $.ajax({
			type : 'post',
			url : 'test.jsp',
			data : {"shopmxid":cn,"state":state,"count":input_num.value},
			success : function(data) {	
			}
		});
   } 
   function buy(){
	   var allprice=document.getElementById("totalprice").innerHTML;
	   //alert(allprice)
	   if(allprice==0){
		   alert("请选择商品")
		   return;
	   }else{
		   var state="7";
		   $.ajax({
				type : 'post',
				url : 'test.jsp',
				data : {"state":state},
				success : function(data) {	
					var id=data; 
					window.location.href="buy.jsp?allprice="+allprice+"&id="+id;
				}
			}); 
	   }  
   }
   function shanchu(b){
	   var state=6;
	   var shopmxid=b.className;
	   $.ajax({
			type : 'post',
			url : 'test.jsp',
			data : {"shopmxid":shopmxid,"state":state},
			success : function(data) {	
				alert("删除成功")
				window.location.reload();
			}
		});
   }
   <%-- 	var x;
       function funt(id){
    	 x=id 
    	 var list=<%=li%>;
    	 console.log(list[id-1].oprice);
    	 var price=list[id-1].oprice;
    	 x=id
         return price; 
        
     } --%>
     /* $(".shopcar-checkbox label").on('touchstart',function(){	
        	var totalprice= document.getElementById("totalprice").innerHTML;
			if($(this).hasClass('shopcar-checkd')){
				$(this).removeClass("shopcar-checkd")
				
			}else{
				$(this).addClass("shopcar-checkd") 	
				
			}
		}) */
	
	<%--  function funt(id){
    	var totalprice= document.getElementById("totalprice").innerHTML;
    	var list=<%=li%>;
      	var price=list[id-1].oprice;
      	if($(".shopcar-checkbox label").hasClass('shopcar-checkd')){
			totalprice=Number(totalprice)+Number(price);
		}else{
			totalprice=Number(totalprice)-Number(price);
		}
	    document.getElementById("totalprice").innerHTML=totalprice; 
     } --%>
      
	</script>
	<footer class="page-footer fixed-footer">
		<div class="shop-go">
			<b style="width:25%;text-align:right">合计：￥</b><b style="width:25%;text-align:left" id="totalprice">0.00</b>
			<span style="width:50%" onclick="buy()">去结算</span>
		</div>
		<ul>
			<li>
				<a href="index.jsp">
					<img src="images/footer001.png"/>
					<p>首页</p>
				</a>
			</li>
			<li>
				<a href="assort.jsp">
					<img src="images/footer002.png"/>
					<p>分类</p>
				</a>
			</li>
			<li class="active">
				<a href="shop.jsp">
					<img src="images/footer03.png"/>
					<p>购物车</p>
				</a>
			</li>
			<li>
				<a href="self.jsp">
					<img src="images/footer004.png"/>
					<p>个人中心</p>
				</a>
			</li>
		</ul>
	</footer>
<%-- <%@ include file="pub/bottom_list31.jsp"%> --%>